function getCssPrefix(PACKAGE = {}) {
  const { name, orgVersion } = PACKAGE;
  if (!name || !orgVersion) {
    return "";
  }
  const _name = name.split("/")[1];
  const _version = orgVersion.split(".").join("-");
  return `${_name}-${_version}`;
}
export const CSSPrefix = getCssPrefix(__PACKAGE__);

export const isObj = (value) =>
  Object.prototype.toString.call(value) == "[object Object]" ? true : false;
export const isArray = (arr) => {
  if (!Array.isArray(arr) || !arr.length) {
    return [];
  }
  return arr;
};
//字符串转对象
export function formatStrToObj(str, defaultReturn) {
  if (isObj(str)) {
    return str;
  }

  try {
    return JSON.parse(str);
  } catch (e) {
    return defaultReturn || {};
  }
}

export const isAbsolutePath = (url) => {
  const reg = /^(http|https|\/\/)/;
  return reg.test(url);
};

export const getText = (val, defaultVal) => {
  if (val !== 0 && !val) {
    return defaultVal || "-";
  }
  if (isObj(val)) {
    return JSON.stringify(val);
  }
  return val;
};

// 获取URI参数
// @param:fields[String,Array]  要取值的key或者key list； 未指定时候会解析所有str
// @param:source[String] 未指定时候默认使用location.search
// ex:
// getUriParams() 获取url上的所有数值
// getUriParams("spm") 获取url上的spm数值
// getUriParams(["spm", "id"]) 获取url上的相关数值
// getUriParams("spm", "https://demo.com?spm=1&id=2") 获取指定字符串中的参数
// getUriParams("spm", "spm=1&id=2") 获取指定字符串中的参数
export function getUriParams(fields, source) {
  let input;

  if (source === undefined) {
    input = (location.search && location.search.slice(1)) || "";
  } else {
    input = source.split("?").pop() || "";
  }

  const output = {};
  if (input.length > 0) {
    input.split("&").forEach((row) => {
      const item = row.split("=");
      output[item[0]] = (item[1] && decodeURIComponent(item[1])) || "";
    });
  }
  if (fields && typeof fields === "string") {
    return output[fields];
  } else if (Object.prototype.toString.call(fields) === "[object Array]") {
    const temp = {};
    fields.forEach((row) => {
      if (typeof row === "string") {
        temp[row] = output[row];
      }
    });
    return temp;
  }
  return output;
}

export const formatPeriod = (type, period) => {
  if (type === "node") {
    const [s, e, nodeId, currentMonth] = period.split(",");
    return {
      period: `${s},${e}`,
      nodeId,
      currentMonth,
    };
  }
  return { period };
};

export const getInputValue = (name) =>
  document.getElementsByName(name).length &&
  document.getElementsByName(name)[0].value;

export function formatUrlParams(obj) {
  const paramsStr = [];
  for (const [key, val] of Object.entries(obj)) {
    let _val = val;
    if (isObj(val)) {
      _val = encodeURIComponent(JSON.stringify(val));
    }
    paramsStr.push(`${key}=${_val}`);
  }
  return paramsStr.join("&");
}
export const renderColumns = (maiTitle = "", description = "") => {
  return (
    <>
      <div className="table-main-title">{maiTitle}</div>
      <div className="table-main-description">{description}</div>
    </>
  );
};

// 转化表格数据
export const extractData = (data, config) => {
  const extractedData = [];

  data.forEach((item) => {
    let extractedItem = {};
    Object.keys(config).forEach((key) => {
      const dataKey = config[key];
      extractedItem.dataRange = key;
      extractedItem.dataCode = dataKey;
      extractedItem = { ...extractedItem, ...item[dataKey] };
      extractedData.push(extractedItem);
    });
  });

  return extractedData;
};
// 转化时间
export const formatTimeRange = (startTime, endTime) => {
  const formattedStartTime = startTime.replace(
    /(\d{4})(\d{2})(\d{2})/,
    "$1.$2.$3"
  );
  const formattedEndTime = endTime.replace(/(\d{4})(\d{2})(\d{2})/, "$1.$2.$3");
  return `${formattedStartTime}-${formattedEndTime}`;
};